package edu.towson.greenteam.offer;

import java.util.ArrayList;

import edu.towson.greenteam.Collection;
import edu.towson.greenteam.Iterator;

public class Offers implements Collection<Offer> {
	
	private ArrayList<Offer> currentOffers;
	private int lastID;

	public Offers() {
		currentOffers = new ArrayList<Offer>();
		lastID = 0;
	}

	// Implement methods from Collection<E> interface
	@Override
	public boolean add(Offer promo) {
		if (this.isEmpty()) {
			currentOffers.add(promo);
			promo.setId(lastID++);
		} else {
			if (this.contains(promo)) {
				return false;
            }
            currentOffers.add(promo);
        }
		return true;
	}

	@Override
	public boolean contains(Offer promo) {
		return currentOffers.contains(promo);
	}

	@Override
	public boolean remove(Offer promo) {
		return currentOffers.remove(promo);
	}

	@Override
	public boolean isEmpty() {
		return currentOffers.isEmpty();
	}

	@Override
	public int size() {
		return currentOffers.size();
	}

	@Override
	public boolean update(Offer promo) {
		Iterator<Offer> i = getItr();
		Offer current;
		while (i.hasNext()) {
			current = i.next();
			if (current.getId()==(promo.getId())) {
				current.setName(promo.getName());
				current.setDescription(promo.getDescription());
				current.setOfferAmt(promo.getOfferAmt());
				return true;
			}
		}
		return false;
	}
	
	// Standard iterator
	public Iterator<Offer> getItr() {
		return new Itr();
	}
	
	private class Itr implements Iterator<Offer> {

	    private int current;

	    public Itr() {
	        this.current = 0;
	    }
	    
		@Override
		public boolean hasNext() {
			return current < currentOffers.size();
		}
		
		public boolean hasPrevious() {
			return current > 0;
		}

		@Override
		public Offer next() {
			if (hasNext() == false) {
				return null;
			} else {
				return currentOffers.get(current++);
            }
		}

		public Offer previous() {
			if (hasPrevious() == false) {
				return null;
			} else {
				return currentOffers.get(--current);
            }
		}
		
	}

}
